import jk.UserDao3;
import org.junit.Test;
import user.Users;
import view.Window;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;


public class Application {
    @Test
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Scanner sc = new Scanner(System.in);
        Users user=new Users();
        UserDao3 dao = new UserDao3();
        String username;
        String password;
        int i;
        while (true) {
            Window.begin();
            int choose = sc.nextInt();
            switch (choose){
                case 1:
                  //  System.out.println("执行通过账号密码查询的方法");
                    System.out.println("请输入登录账号：");
                    username=sc.next();
                    user.setUsername(username);
                    System.out.println("请输入登录密码：");
                    password=sc.next();
                    user.setPassword(password);
                   ResultSet rs=UserDao3.selectbyusernameanpassword(user);
                    if (rs.next()){
                        System.out.println("登录成功");
                    }else{

                        System.out.println("该账号不存在！");
                    }

                    break;
                case 2:
                 //   System.out.println("执行添加用户方法");
                    System.out.println("请输入需要添加的用户：");
                    username=sc.next();
                    user.setUsername(username);
                    System.out.println("请输入用户密码：");
                    password=sc.next();
                    user.setPassword(password);
                    i=UserDao3.insert(user);
                    if (i>0){
                        System.out.println("添加成功！");
                    }else {
                        System.out.println("添加失败！请输入正确的账号！");
                    }
                    break;
                case 3:
                 //   System.out.println("执行修改用户方法");
                    System.out.println("请输入需要修改的账户：");
                    username = sc.next();
                    user.setUsername(username);
                    System.out.println("请输入新的密码：");
                    password = sc.next();
                    user.setPassword(password);
                    i = UserDao3.update(user);
                    if (i>0){
                        System.out.println("修改成功！");
                    }else {
                        System.out.println("修改失败！请输入正确的账号！");
                    }
                    break;
                case 4:
                 //   System.out.println("执行删除用户方法");
                    System.out.println("请输入要删除的账户：");
                    username=sc.next();
                    user.setUsername(username);
                    i=UserDao3.delete(user);
                    if (i>0){
                        System.out.println("删除成功！");
                    }else {
                        System.out.println("删除失败！请输入正确的账号！");
                    }
                    break;
                case 5:
                    // 一个用户一行显示
                 //   System.out.println("执行查询所有用户方法");
                    List<Users> user1=new ArrayList<>();
                    Users ll=new Users();
                    user1=UserDao3.result();

                    for (i = 0; i < user1.size(); i++) {
                        ll = user1.get(i);
                        System.out.println("ID:"+ll.getId()+" "+"USERNAME"+ll.getUsername()+" "+"PASSWORD"+ll.getPassword()+" "+"STATE"+ll.getState());
                    }
                    break;
                default:
                    System.out.println("请输入正确的序号");
            }
        }
    }
}
